package zj.it.bhne.huanxin.fragment;


import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.widget.EaseContactList;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.exceptions.HyphenateException;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import zj.it.bhne.huanxin.R;
import zj.it.bhne.huanxin.myuntils.HuanApplication;


public class MessageFragment extends Fragment {

    private EaseContactList message_list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        initView(view);
        //透明状态栏
        Objects.requireNonNull(getActivity()).getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        View views = new View(getActivity());
        ViewGroup.LayoutParams params = new ViewGroup
                .LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 60);
        views.setLayoutParams(params);
        views.setBackgroundColor(Color.rgb(18, 150, 219));

        ViewGroup decorView = (ViewGroup) getActivity().getWindow().getDecorView();
        decorView.addView(views);

        return view;
    }

    private List<EaseUser> names = new ArrayList<>();
    private Handler handler = new Handler();
    private void initView(View view){
        EaseTitleBar title_bar = view.findViewById(R.id.title_bars);
        title_bar.setTitle("通讯录");
        message_list = view.findViewById(R.id.message_list);
        title_bar.setRightLayoutClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                HuanApplication.setShortToast(getContext(), "添加");
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    List<String> usernames = EMClient.getInstance().contactManager().getAllContactsFromServer();
                    if (usernames!=null){
                        for (int i = 0; i < usernames.size(); i++) {
                            names.add(new EaseUser(usernames.get(i)));
                        }
                    }
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            //初始化时需要传入联系人list
                            message_list.init(names);
                            //刷新列表
                            message_list.refresh();
                        }
                    });
                    //Log.e("TAG", "initView: "+usernames.size() );
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }


}
